//
//  ACNetworkTool.swift
//  swift网络请求封装
//
//  Created by ac on 16/5/22.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestMethod {
    case GET
    case POST
}
struct API {
    static let hostName = "https://api.weibo.com/"
    static let loadAccessToken = "oauth2/access_token"
    //加载用户信息
    static let loadUserInfo = "2/users/show.json"

    static let loadHomeData = "2/statuses/home_timeline.json"
    
}
class ACNetworkTool: AFHTTPSessionManager{
    
    static let sharedTool: ACNetworkTool = {
        let hostNameURL = NSURL(string: API.hostName)
        let tool = ACNetworkTool(baseURL: hostNameURL)
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    func request(method: RequestMethod, URLString: String, parameters: AnyObject?, finish: (responseObject: AnyObject?, error: NSError?) -> Void) {
        if method == .GET {
            GET(URLString, parameters: parameters, progress: nil, success: {
                
                finish(responseObject: $1, error: nil)
                
                }, failure: {
                    
                    finish(responseObject: nil, error: $1)
            })
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: {
                
                finish(responseObject: $1, error: nil)
                
                }, failure: {
                    
                    finish(responseObject: nil, error: $1)
                    
            })
        }
    }
    
    
    
    
}

//封装关于请求用户信息的代码
extension ACNetworkTool{
    //加载token
    func loadAccessToken(code:String,finished:(responseObject:AnyObject?,error:NSError?)->()) {
        
        let urlStr = API.loadAccessToken
        let dict = ["client_id": client_id, "client_secret": client_secret, "grant_type": "authorization_code", "code": code, "redirect_uri": redirect_uri]
        
        self.request(.POST, URLString: urlStr, parameters: dict, finish: finished)
        
    }
    
    
    //加载用户信息
    func loadUserInfo(result: [String : AnyObject],finish:(isSuccess: Bool)->(),finished:(responseObject:AnyObject?,error:NSError?)->()) -> () {
//        guard let result = result as? [String:AnyObject] else{
//            finish(isSuccess: false)
//            
//            return
//        }
        let access_token = result["access_token"]!
        let uid = result["uid"]!
        
        let param = ["access_token": access_token, "uid": uid]
        let urlStr = API.loadUserInfo
        self.request(.GET, URLString: urlStr, parameters: param, finish: finished)
    }

}

//首页相关
extension ACNetworkTool{
    //加载首页微博
    func loadData(since_id: Int64, max_id: Int64, finished:(responseObject: AnyObject?,error:NSError?)->()) -> () {
        
        guard let token = NBUserAccountViewModel.sharedViewModel.userAccount?.access_token else{
            return
        }
        var parameters = ["access_token": token,"count": "10"]
        if since_id > 0 {
            //添加since_id 到参数中
            parameters["since_id"] = "\(since_id)"
        }
        if max_id > 0 {
            //会有bug 会返回max_id 对应的微博
            parameters["max_id"] = "\(max_id-1)"
        }

        self.request(.GET, URLString: API.loadHomeData, parameters: parameters, finish: finished)
    }
}